##################################################################################################################################
#
# Copyright (C) 2008-2013 Mind System Connection. Lda. Part of Daidze Software, created by Siqsuruq. All other rights reserved.
#
# Mind System Connection Webpage: www.msysc.org
# Daidze Software Page: www.daidze.com www.daidze.org
# E-mail: admin@msysc.org, siqsuruq@gmail.com
#
##################################################################################################################################

proc add_warehouse {} {
	set values [$::daidze_main get_values]
	set msg_list [dict create name "Warehouse name is obligatory."]
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		if {[dict get $values def] eq "1"} {
			if {[$::db select_all warehouse * "def=\'1\'"] != ""} {
				tk_messageBox -icon info -type ok -message "[::msgcat::mc "There is already default warehouse. This one will be stored as generic type."]"
				dict set values def 0
			}
		}
		set def_warehouse [dict get $values def_workstation]
		set values [dict remove $values def_workstation]
		$::db insert_all warehouse $values
		
		if {$def_warehouse != 0} {
			write_config_db daidze [set d [dict create workstation [dict create default_warehouse_id [$::db select_last_inserted warehouse id]]]]
		} else {
			write_config_db daidze [set d [dict create workstation [dict create default_warehouse_id 0]]]
		}
		$::daidze_main load_main warehouse warehouse.xml $::main 1
	}
}

proc edit_warehouse {table_var} {
	set sdata [dict create]
	set my_tablelist [dict get [$::daidze_main get_fdata] $table_var]
	set vars {id name contact street street_detail city province country phone mobile fax email}
	set row [$my_tablelist rowcget [$my_tablelist curselection] -text]
	foreach key $vars value $row {
		dict set sdata $key $value
	}
	dict set sdata def [$::db select_all warehouse def "name=\'[dict get $sdata name]\'" list]

	$::daidze_main load_main warehouse edit_warehouse.xml $::main
	$::daidze_main update_form $sdata
}

proc update_warehouse {} {
	set values [$::daidze_main get_values]

	set msg_list [dict create name "Warehouse name is obligatory."]
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		if {[dict get $values def] eq "1"} {
			if {[$::db select_all warehouse * "def=\'1\'"] != ""} {
				tk_messageBox -icon info -type ok -message "[::msgcat::mc "There is already default warehouse. This one will be stored as generic type."]"
				dict set values def 0
			}
		}
		
		set def_warehouse [dict get $values def_workstation]
		set values [dict remove $values def_workstation]
		$::db update_all warehouse $values
		
		if {$def_warehouse != 0} {
			write_config_db daidze [set d [dict create workstation [dict create default_warehouse_id [dict get $values id]]]]
		} else {
			write_config_db daidze [set d [dict create workstation [dict create default_warehouse_id 0]]]
		}
		read_config_db [file join conf $::default_config_db]
		$::daidze_main load_main warehouse warehouse.xml $::main 1
	}
}
